#include "ServerDataMgr.h"

ServerDataMgr* ServerDataMgr::m_ServerData = nullptr;
ServerDataMgr* ServerDataMgr::getInstan()
{
	if (m_ServerData == nullptr)
	{
		m_ServerData = new ServerDataMgr;
	}
	return m_ServerData;
}

void ServerDataMgr::deleteinstan()
{
	if (m_ServerData)
	{
		delete m_ServerData;
		m_ServerData = nullptr;
	}
}

std::map<std::string, std::set<std::string>> ServerDataMgr::getDomainMap()
{
	CAutoLockRead lock(m_statusmut);
	return m_nodestatus;
}

bool ServerDataMgr::getDomainNodeInfo(std::string domain, std::set<std::string>& nodelist)
{
	CAutoLockRead lock(m_statusmut);
	auto finddomain = m_nodestatus.find(domain);
	if (finddomain != m_nodestatus.end())
	{
		nodelist = finddomain->second;
		return true;
	}
	return false;
}

void ServerDataMgr::removeStatusDomain(std::string domain)
{
	CAutoLockWrite lock(m_statusmut);
	m_nodestatus.erase(domain);
}

void ServerDataMgr::removeIndexDomain(std::string domain)
{
	CAutoLockWrite lock(m_indexmut);
	m_domainindex.erase(domain);
}

void ServerDataMgr::removeTimeReqDomain(std::string domain)
{
	CAutoLockWrite lock(m_timereqmut);
	m_nodetimereq.erase(domain);
}

void ServerDataMgr::removeSimTimeDomain(std::string domain)
{
	CAutoLockWrite lock(m_simtimemut);
	m_domainsimtime.erase(domain);
}

void ServerDataMgr::removeSimPushTimeDomain(std::string domain)
{
	CAutoLockWrite lock(m_pushtimemut);
	m_simPushTime.erase(domain);
}

void ServerDataMgr::addNode(std::string domain, std::string nodename)
{
	CAutoLockWrite lock(m_statusmut);
	m_nodestatus[domain].insert(nodename);
}

void ServerDataMgr::removeNode(std::string domain, std::string nodename)
{
	CAutoLockWrite lock(m_statusmut);
	m_nodestatus[domain].erase(nodename);
}

bool ServerDataMgr::checkExistDomain(std::string domain)
{
	CAutoLockRead lock(m_indexmut);
	auto domainitem = m_domainindex.find(domain);
	if (domainitem == m_domainindex.end())
	{
		return false;
	}
	return true;
}

void ServerDataMgr::addDomainFlag(std::string domain, bool val /* = true */)
{
	CAutoLockWrite lock(m_indexmut);
	m_domainindex[domain] = val;
}

bool ServerDataMgr::getDomainFlag(std::string domain)
{
	CAutoLockRead lock(m_indexmut);
	return m_domainindex[domain];
}

bool ServerDataMgr::checkExistDomainSimTime(std::string domain)
{
	CAutoLockRead lock(m_simtimemut);
	auto simitem = m_domainsimtime.find(domain);
	if (simitem == m_domainsimtime.end())
	{
		return false;
	}
	return true;
}

void ServerDataMgr::addDomainSimTime(std::string domain, int val /* = 0 */)
{
	CAutoLockWrite lock(m_simtimemut);
	m_domainsimtime[domain] = val;
}

uint64_t ServerDataMgr::getDomainSimTime(std::string domain)
{
	CAutoLockRead lock(m_simtimemut);
	return m_domainsimtime[domain];
}

bool ServerDataMgr::checkExistDomainPushTime(std::string domain)
{
	CAutoLockRead lock(m_pushtimemut);
	auto tt = m_simPushTime.find(domain);
	if (tt == m_simPushTime.end())
	{
		return false;
	}
	return true;
}

void ServerDataMgr::addDomainPushTime(std::string domain, int timeval /*= 1000*/)
{
	CAutoLockWrite lock(m_pushtimemut);
	m_simPushTime[domain] = timeval;
}

int ServerDataMgr::getDomainPushTime(std::string domain)
{
	CAutoLockRead lock(m_pushtimemut);
	return m_simPushTime[domain];
}

bool ServerDataMgr::checkExistDomainTimeReq(std::string domain)
{
	CAutoLockRead lock(m_timereqmut);
	auto finddomain = m_nodetimereq.find(domain);
	if (finddomain == m_nodetimereq.end())
	{
		return false;
	}
	return true;
}

void ServerDataMgr::addNodeTimeReq(std::string domain, std::string nodename)
{
	CAutoLockWrite lock(m_timereqmut);
	m_nodetimereq[domain].insert(nodename);
}

bool ServerDataMgr::getDomainTimeReqInfo(std::string domain, std::set<std::string>& nodeTimeReq)
{
	CAutoLockRead lock(m_timereqmut);
	auto finddomain = m_nodetimereq.find(domain);
	if (finddomain != m_nodetimereq.end())
	{
		nodeTimeReq = finddomain->second;
		return true;
	}
	return false;
}